package com.fivefu.core.camera.services.impl;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.camera.entity.DbMonitorCameraLabel;
import com.fivefu.core.camera.mapper.DbMonitorCameraLabelMapper;
import com.fivefu.core.camera.services.DbMonitorCameraLabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 探头标签关联表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-20 19:45:47
 */
@Service
public class DbMonitorCameraLabelServiceImpl extends ServiceImpl<DbMonitorCameraLabelMapper, DbMonitorCameraLabel> implements DbMonitorCameraLabelService {

    /**
     * 通过标签编号集合查询关联探头集合
     * @param labels 标签编号集合
     * @return
     */
    @Override
    public Set<String> queryMonitorCameraByLabelIds(List<Long> labels){
        QueryWrapper<DbMonitorCameraLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (labels != null && labels.size() > 0) {
            wrapper.in("label_id", labels);
        }
        wrapper.select("camera_code");
        List<DbMonitorCameraLabel> list = baseMapper.selectList(wrapper);
        Set<String> codeList = list.stream().map(k -> k.getCameraCode()).collect(Collectors.toSet());
        return codeList;
    }

    /**
     *
     * 查询该标签下的探头数量
     * @param id 标签id
     */
    @Override
    public Integer queryMonInLabNum(Long id) {
        QueryWrapper<DbMonitorCameraLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("label_id",id);

        Long count = this.baseMapper.selectCount(wrapper);

        return count.intValue();
    }


    /**
     * 删除标签下探头
     * @param labelId 标签编号
     * @param cameraCodes 探头编号集合
     * @param userId 操作用户编号
     */
    @Override
    public void delMonitorCameraLabelByLabelId(Long labelId, List<String> cameraCodes,String userId) {
        QueryWrapper<DbMonitorCameraLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("label_id",labelId);
        if (cameraCodes != null && cameraCodes.size() > 0){
            wrapper.in("camera_code",cameraCodes);
        }

        DbMonitorCameraLabel label = new DbMonitorCameraLabel();
        label.setIsDelete(1);
        label.setUpdatedBy(userId);
        label.setUpdatedTime(LocalDateTime.now());
        baseMapper.update(label,wrapper);
    }

    /**
     * 通过探头编号查询关联标签信息
     * @param cameraCode 探头编号
     * @return
     */
    @Override
    public List<DbMonitorCameraLabel> queryLabelsByCameraCode(String cameraCode){
        QueryWrapper<DbMonitorCameraLabel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (StrUtils.isNotNull(cameraCode)) {
            wrapper.eq("camera_code", cameraCode);
        }
        List<DbMonitorCameraLabel> list = baseMapper.selectList(wrapper);
        return list;
    }

    /**
     * 探头新增标签
     * @param cameraCode 探头编号
     * @param labelId 标签编号
     * @param userid 操作用户编号
     * @return
     */
    @Override
    public Long insertMonitorCameraLabel(String cameraCode, Long labelId, String userid){
        LocalDateTime time = LocalDateTime.now();
        DbMonitorCameraLabel label = new DbMonitorCameraLabel();
        label.setLabelId(labelId);
        label.setCameraCode(cameraCode);
        label.setCreatedBy(userid);
        label.setCreatedTime(time);
        label.setUpdatedBy(userid);
        label.setUpdatedTime(time);
        label.setIsDelete(0);
        baseMapper.insert(label);

        return label.getId();
    }

    /**
     * 编辑探头标签
     * @param cameraCode 探头编号
     * @param labelList 标签集合
     * @param userid 操作用户
     */
    @Override
    public void editMonitorCameraLabel(String cameraCode, List<Long> labelList, String userid){
        //查询探头当前标签
        List<DbMonitorCameraLabel> labels = queryLabelsByCameraCode(cameraCode);
        Set<Long> labelIds = labels.stream().map(k -> k.getId()).collect(Collectors.toSet());

        if (labelList != null && !labelList.isEmpty()) {
            Map<Long, Long> labelMap = labels.stream().collect(Collectors.toMap(k -> k.getLabelId(), v -> v.getId()));
            for (Long labelId : labelList) {
                if (labelMap.containsKey(labelId)) {
                    labelIds.remove(labelMap.get(labelId));
                } else {
                    insertMonitorCameraLabel(cameraCode, labelId, userid);
                }
            }
        }

        //删除多余标签
        if (labelIds != null && labelIds.size() > 0){
            QueryWrapper<DbMonitorCameraLabel> wrapper = new QueryWrapper<>();
            wrapper.eq("is_delete",0);
            wrapper.in("id",labelIds);

            DbMonitorCameraLabel label = new DbMonitorCameraLabel();
            label.setIsDelete(1);
            label.setUpdatedBy(userid);
            label.setUpdatedTime(LocalDateTime.now());
            baseMapper.update(label,wrapper);
        }
    }

}
